



<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<form action="" method="get">
    <input type="text" name="link_con">
    <input type="submit">
</form>
</body>
</html>



<?php
require 'vendor/Phpquery/autoload.php';
require 'vendor/Qiniu/autoload.php';
require 'mysql.class.php';

use Qiniu\Auth;
use Qiniu\Storage\UploadManager;
use QL\QueryList;

ini_set('date.timezone','Asia/Shanghai');

// 用于签名的公钥和私钥
$qiniu_key = '6tlTXdC9jJqqARNW-oaRtDHesA901SWJ3YJXq9Ty';
$qiniu_security = 'TvFPjkNTJA5xKbxokGOFmWalttH7SW0ty-ubTHrc';
// 初始化签权对象
$auth = new Auth($qiniu_key, $qiniu_security);
$bucket = 'guanwang';
// 初始化签权对象
$auth = new \Qiniu\Auth($qiniu_key, $qiniu_security);
$token = $auth->uploadToken($bucket);
$uploadMgr = new UploadManager();
// qiniu end

//$ml_obj = new lib_mysqli('localhost', 'root', 'root', 'cmfwxapp1', '3306');
$ml_obj = new lib_mysqli('114.115.157.83', 'jinrigaoling', 'jinrigaoling123456', 'jinrigaoling', '3306');

if(!$ml_obj){die('链接失败');}


// 待采集的页面地址
$url1 = 'https://www.meipian8.cn/1zfhshrd?first_share_uid=8116757&share_depth=2&share_source=groupmessage&share_user_mpuuid=6431afe205ed036af998639bb47ca22d&user_id=ohbsluE1kMplaWWkToXwzoEfzyNI&v=4.10.0';
$url2 = 'https://www.meipian3.cn/1z86pil8?first_share_uid=2333341&share_depth=8&share_source=timeline&share_user_mpuuid=44aee00b838c2d2656a8d8a5a563b0dd&user_id=ohbsluE1kMplaWWkToXwzoEfzyNI&v=4.10.0';
$url3 = 'https://www.meipian4.cn/1zyod134?from=timeline&isappinstalled=0&share_from=self&share_user_mpuuid=f7929a92a3354c82667e58d1a126e20a&user_id=52408943&utm_medium=meipian_android&utm_source=timeline&uuid=0991903f3dc2f67eb7ae2ea4f950f53b&v=4.10.1';
$url4 = 'https://www.meipian4.cn/1zty7chn?from=timeline&isappinstalled=0&share_from=self&share_user_mpuuid=f7929a92a3354c82667e58d1a126e20a&user_id=52408943&utm_medium=meipian_android&utm_source=timeline&uuid=0991903f3dc2f67eb7ae2ea4f950f53b&v=4.10.1';
$url5 = 'https://www.meipian4.cn/1zyod134?share_depth=2&user_id=ohbsluE1kMplaWWkToXwzoEfzyNI&first_share_uid=52408943&share_user_mpuuid=f7929a92a3354c82667e58d1a126e20a&v=4.10.1&share_source=timeline';
$url6 = 'https://www.meipian4.cn/1zyod134?share_depth=2&user_id=ohbsluE1kMplaWWkToXwzoEfzyNI&first_share_uid=52408943&share_user_mpuuid=f7929a92a3354c82667e58d1a126e20a&v=4.10.1&share_source=timeline';


// 判断link_con的值
$url5 = $_GET['link_con'];
if(empty($_GET['link_con'])){
echo 'url 为空，程序结束！';
return false;
}
//if(!isset($url)){
//echo 'url 为空，程序结束！';
//return false;
//}


// 采集规则
$rules = [
    // 文章标题
    'title' => ['.well>h1','text'],
    // 文章内容
    'content' => ['.content-container','html']
];

$data = QueryList::Query($url5,$rules)->data;

// $data[0]['content'] 文章内容


//print_r($data[0]['content']);exit;

preg_match_all('/<img.+show-img=\"?(.+)\".+src=\"?(.+)\"?.+>/i',$data[0]['content'],$match);

// https://www.jb51.net/article/134501.htm


//print_r($match);exit;
foreach ($match[1] as $v){
    $data[0]['content']=str_replace($v,'',$data[0]['content']);
}
//print_r($data[0]['content']);
exit;

$k=0;
foreach ($match[2] as $v){
    if(!is_dir('./img')){
        mkdir('./img');
    }
    if(strpos($v,'span')){
        continue;
    }
    $filename = put_file_img('./img',$v);
//     default/20181221/c39d6eb438de3ef01c945171593205c4.jpg
// http://gw.jinrigaoling.com/default/2019-03-25/5c9885a1ca507155349955372006.jpg
    $domain_qi = 'http://gw.jinrigaoling.com/';
    $online_path =  date("Ymd",time());
    $online_path = 'default/'.$online_path;
    // ./img/aa.jpg
    $name_file_arr = explode('/', $filename);
    $tmp_name = $name_file_arr[count($name_file_arr)-1];

    list($ret, $err) = $uploadMgr->putFile($token,$online_path.'/'.$tmp_name, $filename);
    if ($err !== null) {
        var_dump($err);
    } else {
        echo $k++;
        echo  'success!';
    }

    $data[0]['content']=str_replace($v,$online_path.'/'.$tmp_name,$data[0]['content']);

}




/* 抓取保存图片函数
     * return
     * $filename  string  图片地址
     */
function put_file_img($dir='',$image='')
{
    // 判断图片的保存类型 截取后四位地址
    $exts = array('jpeg','png','jpg');
    $filename = $dir.'/'.uniqid().time().rand(10000,99999);
    $imgs = explode('.',$image);
    $ext = $imgs[count($imgs)-1];
    $ext = strstr($ext, '-', true);
    $filename = $filename.'.'.$ext;
//   echo $ext;
    $souce = file_get_contents($image);
    if(file_put_contents($filename,$souce)){
        return $filename;
    }else{
        return false;
    }
}

$table = 'cmf_portal_post';
// create_time 发布时间
// user_id 用户1
// post_status 发布状态
// more
//

$files = array(
    'post_title' =>$data[0]['title'] ,
    'post_content' =>htmlspecialchars($data[0]['content']),
    'create_time'=>time(),
    'user_id'=>1,
    'post_status'=>0
);



$sql = $ml_obj->get_insert_db_sql($table, $files);
$rst = $ml_obj->query($sql);
if($rst){
    echo '数据写入成功！';
}

function put_file_img1($dir='',$image='')
{
    // 判断图片的保存类型 截取后四位地址
    $exts = array('jpeg','png','jpg');
    $filename = $dir.'/'.uniqid().time().rand(10000,99999);
    $imgs = explode('.',$image);
    $ext = $imgs[count($imgs)-1];
    $ext = strstr($ext, '?', true);
    $filename = $filename.'.'.$ext;
//   echo $ext;
    $newimg = strstr($image, '?', true);
    $souce = file_get_contents($newimg);
    if(file_put_contents($filename,$souce)){
        return $filename;
    }else{
        return false;
    }
}


//print_r($data[0]['content']);
//print_r($match);